/*
@author: tk
@file: shopping.go
@time: 2022/11/20 15:23
*/
package main

import (
	"bufio"
	"encoding/json"
	"strconv"

	//"encoding/xml"
	"fmt"
	"os"
	"strings"
)
//直接声明map类型,没有赋值指向的都是空
var user_file = "user.txt"
var user = make(map[string]map[string]string)
var car = make(map[string]int)
var all = 0
var products = map[string]map[string]int{"1":{"苹果":5}, "2": {"香蕉":3}, "3":{"葡萄":8} , "4":{"榴莲":40}}
func main()  {
	init_user()
	//var products = map[string]map[string]interface{}{
	//	"F001":{"name":"apple","price":9},
	//	"F002":{"name":"banana","price":3},
	//	"F003":{"name":"orange","price":4}}
	fmt.Println(strings.Repeat("*", 25),"欢迎来到三乐购物系统",strings.Repeat("*", 25))
	fmt.Println("1.登录")
	fmt.Println("2.注册")
	fmt.Println("(按q退出!)")
	fmt.Print("请输入你的操作:")
	for  {
		flag := 0
		var op string
		fmt.Scan(&op)
		switch op {
		//Loop:
		case "1":
		Loop:
			fmt.Print("输入用户名:")
			Read := bufio.NewReader(os.Stdin)
			name,_ := Read.ReadString('\n')
			fmt.Print("输入用密码:")
			passRead := bufio.NewReader(os.Stdin)
			passwd,_ := passRead.ReadString('\n')
			result := login(strings.Replace(name, "\n", "", -1), strings.Replace(passwd, "\n", "", -1))
			if result {
				fmt.Println("登录成功!")
			}else {
				fmt.Println("用户不存在,或密码错误")
				goto Loop
			}
			Next:
			menuo()
			fmt.Print("请输入操作:")
			var opt string
			fmt.Scan(&opt)
			switch opt {
			case "1":
				menut()
				fmt.Println(strings.Repeat("*",20))
				goto Next
			case "2":
				fmt.Print("请输入你要添加的商品编号:")
				var fruitNum string
				fmt.Scan(&fruitNum)
				fmt.Print("请输入购买数量:")
				var fruitMuch int
				fmt.Scan(&fruitMuch)
				addCar(fruitNum,fruitMuch)
				goto Next
			case "3":
				for keyEnd,valueEnd := range car {
					check(keyEnd,valueEnd)
				}
				fmt.Println("一共是:",all,"元")
				var peopleMoney = user[strings.Replace(name, "\n", "", -1)]["money"]
				var Money = 0
				var Moneys = ""
				//var Moneyt = ""
				Money,_ =strconv.Atoi(peopleMoney)
				fmt.Println(Money)
				if Money<all {
					fmt.Println("您的余额不足!!")
				}else {
					Money = Money - all
					fmt.Println(Money)
					fmt.Println("结算成功!")
					Moneys = strconv.Itoa(Money)
					shop(name,Moneys)
					fmt.Println(user)
					goto Next
				}
			case "4":
				flag = 1
			}
		case "2":
			fmt.Println("输入用户名:")
			Read := bufio.NewReader(os.Stdin)
			name,_ := Read.ReadString('\n')
			fmt.Println("输入用密码:")
			passRead := bufio.NewReader(os.Stdin)
			passwd,_ := passRead.ReadString('\n')
			result := regist(strings.Replace(name, "\n", "", -1), strings.Replace(passwd, "\n", "", -1))
			//fmt.Println(result)
			if result {
				fmt.Println("注册成功!")
			}

		case "q":
			flag = 1
		}
		if flag==1 {
			break
		}
	}
}

func login(name string,passwd string) bool {
	_, ok := user[name]
	if ok && user[name]["passwd"]==passwd{
		return true
	}else {
		return false
	}

}
func regist(name string,passwd string) bool {
	_, ok := user[name]
	if ok {
		fmt.Println("用户已存在,请重输")
		return false
	}else {
		var money string
		fmt.Println("冲多少?")
		fmt.Scan(&money)
		var name_map = map[string]string{
			"passwd":passwd,
			"money":money,
		}
		user[name]=name_map //要使用make给空map分配存储空间
		//fmt.Println("用户信息写入文件")
		WriteUserInfo()
		return true
	}
}
func init_user()  {
	content,err := os.ReadFile(user_file) // 只读方式打开文件
	 if err != nil{
	 	fmt.Println("初始化用户信息失败,没有指定文件")
	 }else {
		  err1 := json.Unmarshal(content,&user)
		 if err1 != nil {
		 	fmt.Println("json格式化失败,初始化用户信息失败")
		 }
		 }
}
func WriteUserInfo()  {
	file,err := os.OpenFile(user_file, os.O_CREATE|os.O_WRONLY,666)
	if err != nil{
		fmt.Println("打开文件,写入用户信息失败")
	}else {
		str_m1, _ := json.Marshal(user)
		file.Write(str_m1)
		//fmt.Println("写入文件成功")
	}
}
func menuo()  {
	fmt.Println("1、查看商品列表")
	fmt.Println("2、添加到购物车")
	fmt.Println("3、结算")
	fmt.Println("4、退出")
}
func menut()  {
	fmt.Println("编号","名称","价格")
	for key, value := range products {
		for keys, values := range value {
			fmt.Println(key,keys,values)
		}
	}
}
func addCar(fruitNum string,fruitMuch int)  {
	car[fruitNum] = fruitMuch
	fmt.Println(car)
	fmt.Println("添加成功")
}
func check(keyEnd string,valueEnd int)  {
	for _, value := range products[keyEnd] {
		sice := valueEnd*value
		all = sice + all
	}
}
func shop(name string,Money string)  {
	_, ok := user[strings.Replace(name, "\n", "", -1)]
	if ok{
		user[strings.Replace(name, "\n", "", -1)]["money"]=Money
	}
	WriteUserInfo()
}